package org.afs.gescol.edt.api.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * App utilities for EDT API.
 * @author Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public final class EdtUtilities {

    private EdtUtilities() {
    }

    /**
     * To determine if a string si numeric or not.
     * @param str the string
     * @return true or false
     */
    public static boolean isNumeric(String str) {
        new EdtUtilities();
        return str.matches("-?\\d+(.\\d+)?");
    }

    /**
     * Group successive integer elements in a list to the list of arrays.
     * @param list the integer list
     * @return list of arrays
     */
    public static List<Integer[]> groupIntegerList(List<Integer> list) {
        Collections.sort(list);
        final List<Integer[]> grouped = new ArrayList<Integer[]>();
        if (list != null) {
            for (int i = 0; i < list.size();) {
                final Integer first = list.get(i);
                Integer h1 = first;
                Integer second = first;
                for (int j = ++i; j < list.size(); j++) {
                    final Integer h2 = list.get(j);
                    if (h2.equals(++h1)) {
                        second = h2;
                        i++;
                    } else {
                        break;
                    }
                }
                grouped.add(new Integer[]{first, second});
            }
        }
        return grouped;
    }
}
